// components/exchange-popup/exchange-popup.js
Component({
  properties: {
    show: {
      type: Boolean,
      value: false
    },
    unitPrice: {
      type: Number,
      value: 1500
    },
    availableTickets: {
      type: Number,
      value: 0
    }
  },
  data: {
    count: 1
  },
  methods: {
    increase() {
      if (this.data.count * this.properties.unitPrice < this.properties.availableTickets) {
        this.setData({ count: this.data.count + 1 });
      }
    },
    decrease() {
      if (this.data.count > 1) {
        this.setData({ count: this.data.count - 1 });
      }
    },
    onClose() {
      this.triggerEvent('close');
    },
    onConfirm() {
      const totalCost = this.properties.unitPrice * this.data.count;
      if (totalCost > this.properties.availableTickets) {
        wx.showToast({
          title: '星票不足',
          icon: "error"
        });
        return;
      }
      this.triggerEvent('confirm', { count: this.data.count });
    }
  }
});
